using UnityEngine;
using System.Collections;

public class ItemManager : MonoBehaviour 
{
    public GameObject item01;
    public GameObject item01mirror;
    public GameObject item02;
    public GameObject item02mirror;
    public bool haveSeen;
    public bool isComplete;
    public bool isMoving;

    public enum options {Door,Swap,Move,Shoot,Rotate}
    public options objective;
    public GameObject objectiveObject;
    public GameObject objectiveObject2;
    public GameObject exit;
    public Vector3 moveDirection;
    public Vector3 rotation;
    public bool useOnce;
    public Transform bullet;

    private Material tempMat1;
    private Material tempMat2;
    private bool haveCalled;

    void Update()
    {
        if ((item01.GetComponent<ItemVisibility>().isVisible || (item01mirror != null && item01mirror.GetComponent<ItemVisibility>().isVisible)) && (item02.GetComponent<ItemVisibility>().isVisible || (item02mirror != null && item02mirror.GetComponent<ItemVisibility>().isVisible)))
        {
            haveSeen = true;
        }
        else
        {
            haveSeen = false;
        }

        if (Input.GetKeyDown(KeyCode.E))
        {
            if(haveSeen && !isComplete && ItemCount.numberOfItems < 3)
            {
                SeenItems();
            }
        }

        if (haveSeen && !isComplete && ItemCount.numberOfItems < 3 &&  !haveCalled)
        {
            haveCalled = true;
            ++GUIController.seeCount;
        }

        if ((!haveSeen || isComplete || ItemCount.numberOfItems > 2) && haveCalled)
        {
            haveCalled = false;
            --GUIController.seeCount;
        }
    }

    void SeenItems()
    {
        
        if (objective == options.Door)
        {
            if(item01.GetComponent<ItemVisibility>().isActive && item02.GetComponent<ItemVisibility>().isActive)
            {
                isComplete = true;
                OpenDoor();
            }
        }

        if (objective == options.Swap)
        {
            Swap();
        }

        if (objective == options.Move)
        {
            if (item01.GetComponent<ItemVisibility>().isActive && item02.GetComponent<ItemVisibility>().isActive)
            {
                Move();
            }
        }

        if (objective == options.Shoot)
        {
            Shoot();
        }

        if (objective == options.Rotate)
        {
            Rotate();
        }
    }

    void Move()
    {
        if (useOnce)
        {
            isComplete = true;
        }

        item02.transform.position = item02.transform.position + moveDirection;
    }

    void Swap()
    {
        tempMat1 = item01.GetComponent<MeshRenderer>().material;
        tempMat2 = item02.GetComponent<MeshRenderer>().material;

        item01.GetComponent<MeshRenderer>().material = tempMat2;
        item02.GetComponent<MeshRenderer>().material = tempMat1;
    }

    void OpenDoor()
    {
        Debug.Log("DOOR OPEN");
        objectiveObject.GetComponent<MeshRenderer>().renderer.enabled = false;
        if (objectiveObject2 != null)
        {
            objectiveObject2.GetComponent<MeshRenderer>().renderer.enabled = false;
        }
        exit.GetComponent<BoxCollider>().isTrigger = true;
    }

    void Shoot()
    {
        if (useOnce)
        {
            isComplete = true;
        }
        Instantiate(bullet, item02.transform.position, item02.transform.rotation);
    }

    void Rotate()
    {
        item02.transform.Rotate(Vector3.up * 45);
    }
}
